package vip.liux.backend.infrastructure.code;

import org.apache.commons.lang3.StringUtils;
import org.springframework.plugin.core.PluginRegistry;
import org.springframework.stereotype.Component;
import vip.liux.cache.code.CodeCategory;
import vip.liux.cache.code.CodeGenerator;

@Component
public class BizCodeGenerator {

    private final CodeGenerator codeGenerator;

    public BizCodeGenerator(PluginRegistry<CodeGenerator, CodeCategory> pluginRegistry) {
        this.codeGenerator = pluginRegistry.getRequiredPluginFor(CodeCategory.REDIS);
    }

    public String generatePropNo() {
        return "P-" + codeGenerator.generateSerialNumber("PROP");
    }

    public String generateBrandNo() {
        return "BD" + codeGenerator.generateSerialNumber("BRAND");
    }

    public String generateCouponNo() {
        return "C-" + codeGenerator.generateSerialNumber("COUPON");
    }

    public String generateUserCouponCode() {
        return "UC-" + codeGenerator.generateSerialNumber("USER_COUPON");
    }

    public String generateTemplateCode() {
        return "MTC-" + codeGenerator.generateSerialNumber("MessageTemplate");
    }

    public String generateSpu(String catParentNo, String catNo) {
        return "P." + StringUtils.substringAfterLast(catParentNo, ".") + "." + StringUtils.substringAfterLast(catNo, ".") + "." + codeGenerator.generateSerialNumber("SPU");
    }

    public String generateSku(String catParentNo, String catNo) {
        return "K." + StringUtils.substringAfterLast(catParentNo, ".") + "." + StringUtils.substringAfterLast(catNo, ".") + "." + codeGenerator.generateSerialNumber("SKU");
    }

    public String generateFirstCatNo() {
        return "C." + codeGenerator.generateSerialNumberNext("CAT:root", "%1$02d");

    }

    public String generateSecondCatNo(String parentNo) {
        return parentNo + "." + codeGenerator.generateSerialNumberNext("CAT:" + parentNo, "%1$03d");
    }

    public String generateThirdCatNo(String parentNo) {
        return parentNo + "." + codeGenerator.generateSerialNumberNext("CAT:" + parentNo, "%1$04d");
    }

    public String generateTransactionNo() {
        return "T." + codeGenerator.generateSerialNumber("TRANSACTION");
    }

    public String generatePayNo() {
        return "PAY-NO-" + codeGenerator.generateSerialNumber("PAY_NO");

    }

    public String generateRefundNo() {
        return "REFUND-NO-" + codeGenerator.generateSerialNumber("REFUND_NO");
    }

    public String generateWalletTransaction() {
        return "WP-" + codeGenerator.generateSerialNumber("WALLET_TRANSACTION");
    }

    public String generateTransferNo() {
        return "T-" + codeGenerator.generateSerialNumber("TRANSFER_NO");
    }
}
